package com.zbkj.admin.controller.platform;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbkj.common.model.AiBackground;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.AiBackgroundRequest;
import com.zbkj.common.request.AiBackgroundSearchRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.AiBackgroundService;
import com.zbkj.service.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * ai试衣背景图表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/ai-background")
@Api(tags = "ai试衣背景图")
public class AiBackgroundController {

    @Autowired
    private AiBackgroundService aiBackgroundService;
    @Autowired
    private SystemAttachmentService systemAttachmentService;


    /**
     * 分页显示ai试衣背景图表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author keNan
     * @since 2025-05-21
     */
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<AiBackground>> getList(@Validated AiBackgroundSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<AiBackground> aiBackgroundCommonPage = CommonPage.restPage(aiBackgroundService.getList(request, pageParamRequest));
        return CommonResult.success(aiBackgroundCommonPage);
    }

    /**
     * 新增ai试衣背景图表
     *
     * @param aiBackgroundRequest 新增参数
     * @author keNan
     * @since 2025-05-21
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated AiBackgroundRequest aiBackgroundRequest) {
        AiBackground aiBackground = new AiBackground();
        BeanUtils.copyProperties(aiBackgroundRequest, aiBackground);
        aiBackground.setCreateTime(new Date())
                .setAiBackgroundUrl(systemAttachmentService.clearPrefix(aiBackgroundRequest.getAiBackgroundUrl()));
        if (1 == aiBackgroundRequest.getIsDefault()) {
            LambdaUpdateWrapper<AiBackground> luw = new LambdaUpdateWrapper<>();
            luw.set(AiBackground::getIsDefault, 0);
            luw.eq(AiBackground::getIsDefault, 1);
            aiBackgroundService.update(luw);
        }
        if (aiBackgroundService.save(aiBackground)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除ai试衣背景图表
     *
     * @param id Integer
     * @author keNan
     * @since 2025-05-21
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (aiBackgroundService.removeById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改ai试衣背景图表
     *
     * @param aiBackgroundRequest 修改参数
     * @author keNan
     * @since 2025-05-21
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated AiBackgroundRequest aiBackgroundRequest) {
        AiBackground aiBackground = new AiBackground();
        BeanUtils.copyProperties(aiBackgroundRequest, aiBackground);
        aiBackground.setAiBackgroundUrl(systemAttachmentService.clearPrefix(aiBackground.getAiBackgroundUrl()));
        if (aiBackgroundService.updateById(aiBackground)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询ai试衣背景图表信息
     *
     * @param id Integer
     * @author keNan
     * @since 2025-05-21
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<AiBackground> info(@RequestParam(value = "id") Integer id) {
        AiBackground aiBackground = aiBackgroundService.getById(id);
        return CommonResult.success(aiBackground);
    }


}



